package com.uenpay.im.ui.old.activity.chat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.database.DBFriendColumns;
import com.qyx.android.database.DBTopMsgColumns;
import com.qyx.android.database.FriendDB;
import com.qyx.android.database.TalkMsgManager;
import com.qyx.android.database.TopListMsgManager;
import com.qyx.android.entity.FriendEntity;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.contacts.ReportContactsOrGroupActivity;
import com.uenpay.im.ui.old.activity.group.AddGroupMemberActivity;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.im.weight.RoundImageView;
import com.uenpay.util.app.base.BaseActivity;

import java.text.MessageFormat;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class TalkPersonalDetailActivity extends BaseActivity {

    private TalkMsgManager takeMsgManager = new TalkMsgManager();
    private TopListMsgManager topListMsgManager = new TopListMsgManager();
    private String chatId;
    private RoundImageView avatar;
    private ImageView add_friends, remind_msg_iv;
    private String name = "";
    private FriendEntity friendEntity = null;
    private int is_remind = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_talk_person_detail_layout;
    }

    @Override
    public void initView(View view) {
        chatId = getIntent().getStringExtra("to_id");
        name = getIntent().getStringExtra("name");
        initView();
        initListener();
        initData();
        avatar.SetUrl(APIConfiguration.getAvatarUrlNormal(chatId,
                PacketType.PERSONAL));
        ((TextView) findViewById(R.id.name)).setText(name);
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.talk_setting));
        avatar = (RoundImageView) findViewById(R.id.user_avatar);
        add_friends = (ImageView) findViewById(R.id.add_friends);
        remind_msg_iv = (ImageView) findViewById(R.id.remind_msg_iv);
    }

    protected void initListener() {
        //返回键
        findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        add_friends.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Bundle bundle = new Bundle();
                bundle.putString("talkId", chatId);
                Utils.startActivity(TalkPersonalDetailActivity.this,
                        AddGroupMemberActivity.class, bundle);
                finish();
            }
        });

        avatar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (!TextUtils.isEmpty(chatId)) {
                    ActivityHandle.startUserDetailActivity(
                            TalkPersonalDetailActivity.this, chatId);
                }
            }
        });
        findViewById(R.id.report_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        if (!TextUtils.isEmpty(chatId)) {
                            Intent intent = new Intent();
                            intent.putExtra("to_cust_id", chatId);
                            intent.putExtra("type", 1);
                            intent.setClass(TalkPersonalDetailActivity.this,
                                    ReportContactsOrGroupActivity.class);
                            startActivity(intent);
                        }
                    }
                });
        findViewById(R.id.clear_messages).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        clearMsgDialog();
                    }
                });
        remind_msg_iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (is_remind == 0) {
                    is_remind = 1;
                    remind_msg_iv
                            .setImageResource(R.drawable.img_button_toggle_on);
                } else {
                    is_remind = 0;
                    remind_msg_iv
                            .setImageResource(R.drawable.img_button_toggle_off);
                }
                setFriendMsgPush();
            }
        });

    }

    private void initData() {
        friendEntity = FriendDB.getFriend(chatId);
        if (friendEntity != null) {
            is_remind = friendEntity.is_remind;
            if (is_remind == 1) {
                remind_msg_iv.setImageResource(R.drawable.img_button_toggle_on);
            } else {
                remind_msg_iv
                        .setImageResource(R.drawable.img_button_toggle_off);
            }
        } else {
            findViewById(R.id.is_remind_layout).setVisibility(View.GONE);
        }
    }

    private void clearMsgDialog() {
        DialogUtility.showDialog(TalkPersonalDetailActivity.this, MessageFormat
                        .format(getResources().getString(
                                R.string.delete_messages_log_personal_in_talk), name),
                R.string.yes, R.string.no, true, new IOnBottomDialogListener() {

                    @Override
                    public void onClicked() {
                        delDBmsg();
                    }
                });
    }

    public void delDBmsg() {

        takeMsgManager.deleteMsgByFromCustIdAndToCustId(
                Long.valueOf(IMApplication.getCustId()), Long.valueOf(chatId));
        topListMsgManager
                .deleteByMsg(Long.valueOf(chatId), PacketType.PERSONAL);

        topListMsgManager.updateTopMsgByField(Long.valueOf(chatId),
                PacketType.PERSONAL, DBTopMsgColumns.CONTENT, "");

        Intent intent = new Intent();
        intent.setAction(BroadcastAction.CLEAR_MSG_ACTION);
        sendBroadcast(intent);

        Intent update_top = new Intent(BroadcastAction.CLEAR_TALK_MSG_ACTION);
        sendBroadcast(update_top);
        IMApplication.showToast(getResources().getString(
                R.string.delete_completed));
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void setFriendMsgPush() {
        if (friendEntity != null) {
            FriendDB.updateFriend(chatId, DBFriendColumns.IS_REMIND, is_remind
                    + "");
        }

    }

}
